package com.sna.snaapplication.controller.UserSystem;



import com.sna.snaapplication.entity.R;
import com.sna.snaapplication.entity.usercore.login.LoginBody;
import com.sna.snaapplication.mapper.UserMapper;
import com.sna.snaapplication.service.Captcha.CaptchaService;
import com.sna.snaapplication.service.LogReg.LoginService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Validated//启动注解校验
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private CaptchaService captchaService;
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/login")
    public R login(@RequestBody LoginBody loginBody, HttpSession session, HttpServletRequest httpRequest){

        // 先验证验证码
        if (!captchaService.validateCaptcha(httpRequest , loginBody.getCaptcha())) {
            return R.fail("验证码错误或已过期");
        }
        System.out.println(
                "controller层："+
                        "username:"+loginBody.getUsername()+"password:"+loginBody.getPassword()
        );
        return loginService.login(loginBody,session);
    }
}
